import axios from "axios";

import {ElMessage, ElNotification} from "element-plus"

const instance = axios.create({
    baseURL:"",
    timeout:1000 * 3,

})

// 请求拦截
instance.interceptors.request.use(config => {
    return config
},error => {
    console.log(error);
    Promise.reject(error)  
})

// 响应拦截
instance.interceptors.response.use((response) => {
    const code = response.data.code
    const msg = response.data.msg

    if(code == 500){
        ElMessage.error(msg)
        return Promise.reject(new Error(msg))
    }else if(code != 200){
        ElNotification({
            title:msg,
            type:"error"
        })
        return response
    }
    return response
    
},error => {
    console.log("err" + error);

    let {message} = error

    if(message == 'Network Error'){
        message = "后端接口异常"
    }else if(message.includes('timeout')){
        message = "系统接口请求超时"
    }else if (message.includes("Request failed with status code")) {
        message = "系统接口" + message.substr(message.length - 3) + "异常";
    }
    ElMessage({
        type:"error",
        message,
        duration:5 * 1000
    })
    
}

)

export default instance